# -*- coding: utf-8 -*-
# @Time    : 2024/7/15 15:20
# @Author  : floraachy
# @File    : repo_new_milestone_page
# @Software: PyCharm
# @Desc:

# 标准库导入
# 第三方库导入
import allure
# 本地应用/模块导入
from utils.base_utils.base_page import BasePage


class RepoNewMilestonePage(BasePage):
    """新建里程碑页面"""
    # 里程碑名称
    locator_milestone_name_input = "//input[@id='NewMilepostFrom_name']"
    # 里程碑描述
    locator_milestone_desc_input = "//textarea[@id='NewMilepostFrom_description']"
    # 截止日期 - 年份
    locator_year_div = "(//div[@class='ant-select-selection-selected-value'])[1]"
    # 年份下拉框值
    locator_year_value = "//li[text()='{}']"
    # 截止日期 - 月份
    locator_month_div = "(//div[@class='ant-select-selection-selected-value'])[2]"
    # 月份下拉框值
    locator_month_value = "//li[text()='{}月']"
    # 日期值
    locator_date_value = "//td[@title='{}年{}月{}日']//div[text()='{}']"
    # 创建里程碑按钮
    locator_create_milestone_btn = "//a[text()='创建里程碑']"

    @allure.step("访问新建里程碑页：/{repo_owner}/{repo_identifier}/milestones/new")
    def navigate(self, repo_owner, repo_identifier):
        """
        访问新建里程碑页
        """
        self.visit(f"/{repo_owner}/{repo_identifier}/milestones/new")

    @allure.step("输入里程碑名称：{name}")
    def input_milestone_name(self, name):
        """
        输入里程碑名称
        """
        self.input(locator=self.locator_milestone_name_input, text=name)

    @allure.step("输入里程碑描述：{desc}")
    def input_milestone_desc(self, desc):
        """
        输入里程碑描述
        """
        self.input(locator=self.locator_milestone_desc_input, text=desc)

    @allure.step("选择里程碑截止日期：{deadline}")
    def input_milestone_deadline(self, deadline):
        """
        选择里程碑截止日期
        """
        year = deadline.split("-")[0]
        month = int(deadline.split("-")[1])
        date = deadline.split("-")[2]
        # 点击 年份，出现年份下拉框
        self.click(self.locator_year_div)
        # 选择年
        self.click(self.locator_year_value.format(year))
        # 点击 月份，出现月份下拉框
        self.click(self.locator_month_div)
        # 点击月
        self.click(self.locator_month_value.format(month))
        # 点击具体的日期
        self.click(self.locator_date_value.format(year, month, date, date))

    @allure.step("点击【创建里程碑】")
    def click_create_milestone_button(self):
        """点击【创建里程碑】"""
        self.click(self.locator_create_milestone_btn)

    # --------------------- 流程 -------------------------------------
    @allure.step(
        "新建里程碑流程：  里程碑名称：{name}，里程碑描述：{desc}，里程碑截止日期：{deadline}")
    def new_milestone_flow(self, name, desc, deadline=None):
        """
        新建里程碑流程
        """
        self.input_milestone_name(name)
        self.input_milestone_desc(desc)
        if deadline:
            self.input_milestone_deadline(deadline)

        self.click_create_milestone_button()
        self.wait(1)
